﻿Public Partial Class ChangePassword
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Dim clsEncrypt As New Class_Encrypt
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModalChange();", True)
    End Sub

    Private Function chkParameter() As Boolean
        If (txtPassold.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุ Old Password!!');", True)
            Return False
        End If
        If Not (txtPass1.Text.Trim() = "") And (txtPass2.Text.Trim() = "") Then
            If ((txtPass1.Text.Trim() = "") Or (txtPass2.Text.Trim() = "")) Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุ New Password!!');", True)
                Return False
            End If
            If (txtPass1.Text.Trim() <> txtPass2.Text.Trim()) Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('รหัสผ่านไม่ถูกต้อง !!');", True)
                Return False
            End If
            If Not Regex.IsMatch(txtPass1.Text.ToLower().Trim, "^[a-zA-Z0-9]*$") Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('รูปแบบ Password ไม่ถูกต้อง !!');", True)
                Return False
            End If
        End If
        Dim ds As DataSet = clsGet.Get_Password(CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("User_name").ToString.Trim())
        If Not (ds.Tables(0).Rows.Count = 0) Then
            If (txtPassold.Text.Trim().ToLower() = clsEncrypt.Decrypt(ds.Tables(0).Rows(0).Item("Password").ToString())) Then
                Return True
            Else
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('Old Password ไม่ถูกต้อง !!');", True)
                Return False
            End If
        End If
        Return False
    End Function


    Private Sub BtnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSave.Click
        If (chkParameter()) Then
            Dim conn As New System.Data.SqlClient.SqlConnection(connString)
            Dim clsDB As New ClassDatabase
            Dim clsFc As New ClassFunction
            Dim condition As Boolean = True
            Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
            Dim Pass As String = clsEncrypt.Encrypt(txtPass1.Text.ToLower().Trim())
            Application.Lock()
            conn.Open()
            Dim A_Code As String = clsDB.GeneratorID(conn, "Action_Operation")
            tran = clsDB.BeginTran(conn)

            If (clsFc.UpdateUserPassword(CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("User_name"), Pass, conn, tran) < 1) Then
                condition = False
            End If
            If (condition) Then
                clsDB.CommitTran(tran)
                'ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModalTheme();", True)
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('ทำการเปลี่ยน Password เรียบร้อบ!!');parent.location.reload();", True)

            Else
                clsDB.RollBackTran(tran)
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึก');", True)
            End If
            Application.UnLock()
            conn.Dispose()
        End If

    End Sub
End Class